******created 7/8/19 for the JOHR submission ******************************
clear
cd [insert file path]
use "SERF_Disaster_Media_2019replicationfile", clear

xtset ccode year

*first have to create a lag on the variables because of a Stata bug 
gen polity2_tm5=l5.polity2
gen FreeMedia_tm5=l5.FreeMedia
gen internetusers_tm5=l5.internetusers
gen inttot_tm1=l.inttot
gen civtot_tm1=l.civtot
gen fdi_tm5=l5.fdi
gen merch_tm5=l5.merch
gen popdens_tm1=l.popdens
gen BigDisaster_tm5=l5.BigDisaster

*creating Table 1: Variables and Summary Statistics 

xtreg CORE_SERF polity2_tm5 FreeMedia_tm5 internetusers_tm5 inttot_tm1 civtot_tm1 fdi_tm5 merch_tm5 popdens_tm1, fe
gen in_model = e(sample)
estpost sum CORE_SERF polity2_tm5 FreeMedia_tm5 internetusers_tm5 inttot_tm1 civtot_tm1 fdi_tm5 merch_tm5 popdens_tm1 BigDisaster_tm5 if in_model==1
esttab, cell("mean sd min max") nonumber nomtitle
esttab using means.rtf, modelwidth(10 20) cell((mean(label(Mean)) sd(par label(Standard Deviation)) min(label(Minimum)) max(label(Maximum)))) label nomtitle nonumber replace

*creating Table 2: When Media Matter - The Effects of MF and Internet on SERF 2005-2015
eststo: quietly xtreg CORE_SERF l5.polity2 l5i.FreeMedia l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens, fe
eststo: quietly xtreg CORE_SERF l5.polity2 l5i.FreeMedia l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens, fe
eststo: quietly xtreg CORE_SERF l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens, fe
eststo: quietly xtreg CORE_SERF l.CORE_SERF l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens, fe
eststo: quietly xtreg CORE_SERF l5.polity2 l5i.FreeMedia  l5c.internetusers##l5i.BigDisaster l.inttot l.civtot l5.fdi l5.merch l.popdens, fe
esttab using "SERF_BD_MF_models.rtf", replace label nogap onecell se r2 bic
eststo clear

*creating Figure 2: The Effect of MF and BD on SERF ...  
xtreg CORE_SERF  polity2_tm5 FreeMedia_tm5##BigDisaster_tm5 internetusers_tm5 inttot_tm1 civtot_tm1 fdi_tm5 merch_tm5 popdens_tm1, fe
margins FreeMedia_tm5, at (polity2_tm5=(-10(1)10) BigDisaster_tm5=1) 
marginsplot, recast(scatter) level(90)

*creating Figure 3: The Effect of Media Freedom and MND on SERF ...  
xtreg CORE_SERF  polity2_tm5 FreeMedia_tm5##BigDisaster_tm5 internetusers_tm5 inttot_tm1 civtot_tm1 fdi_tm5 merch_tm5 popdens_tm1, fe
margins FreeMedia_tm5, at (polity2_tm5=(-10(1)10) BigDisaster_tm5=0) 
marginsplot, recast(scatter) level(90)

*creating Figure 4: Better Together - The Effects of MF, MND and Internet
ssc install coefplot, replace
help coefplot

xtreg CORE_education l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens if in_model==1, fe
estimates store education
xtreg CORE_health l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens if in_model==1, fe
estimates store health
xtreg CORE_housing l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens if in_model==1, fe
estimates store housing
xtreg CORE_food l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens if in_model==1, fe
estimates store food
xtreg CORE_work l5.polity2 l5i.FreeMedia##l5i.BigDisaster l5c.internetusers l.inttot l.civtot l5.fdi l5.merch l.popdens if in_model==1, fe
estimates store work
coefplot education health housing food work, nolabel drop(L.inttot L.civtot L5.fdi L5.merch L.popdens 0bL5.FreeMedia 0bL5.BigDisaster 0bL5.FreeMedia#0bL5.BigDisaster 0bL5.FreeMedia#1oL5.BigDisaster 1oL5.FreeMedia#0bL5.BigDisaster _cons) xline(0)



